package xyz.xtt.common.objects;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

/**
 * 对象拷贝
 * 
 * @author huqibo
 * @date 2025/04/06
 */
public class CopyUtils {
	public static <T> T copyObj(Object source, Class<T> clazz) {
		if (source == null) {
			return null;
		}
		T obj = null;
		try {
			obj = clazz.getDeclaredConstructor().newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		BeanUtils.copyProperties(source, obj);
		return obj;
	}

	/**
	 * 列表复制
	 */
	public static <T> List<T> copyList(List<Object> source, Class<T> clazz) {
		List<T> target = new ArrayList<>();
		if (!CollectionUtils.isEmpty(source)) {
			for (Object c : source) {
				T obj = copyObj(c, clazz);
				target.add(obj);
			}
		}
		return target;
	}
}
